package thread;

public class ThreadDemo13 {
    public static int count = 0;
    public static void main(String[] args) throws InterruptedException {
        //在使用synchronized必须要有一个对象
        Object locked = new Object();
        //用俩个线程实现从0到100_0000的相加
        Thread t = new Thread(() -> {

           for(int i = 0; i < 50_0000; i++){
               synchronized (locked){
                   count++;
               }
           }
        });

        Thread tt = new Thread(() -> {
            for(int i = 50_0000; i < 100_0000 ; i++){
                synchronized (locked){
                    count++;
                }
            }
        });
        //启动俩个线程开始计数
        t.start();
        tt.start();
        //让线程等待没算好的线程
        t.join();
        tt.join();
        System.out.println("count的值为:"+count);
    }
}
